package com.personal.sfe.test;

import com.personal.sfe.user.entity.User;
import com.personal.sfe.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author: 梁奔
 * @date: 2025/11/04
 */
@Component
@RequiredArgsConstructor
public class DataInitializer implements CommandLineRunner {
    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) {
        if (userService.getUserByUserName("admin") == null) {
            User user = new User();
            user.setUsername("admin");
            user.setPassword(passwordEncoder.encode("admin123"));
            user.setEnabled(true);
            userService.save(user);
            System.out.println("✅ 初始化用户: admin / admin123");
        }
    }
}
